package com.qianfeng.index_7005.service.impl;

import com.qianfeng.entity.Type;
import com.qianfeng.index_7005.dao.TypeDao;
import com.qianfeng.index_7005.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * author: 田晋宇
 * datetime:2021/11/2 15:03
 * email:853796359@qq.com
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Resource
    private RedisTemplate <String,Object> redisTemplate;

    @Autowired
    private TypeDao typeDao;

    @Override
    public List<Type> getFirst() {

        Object types = redisTemplate.opsForValue().get("types");
        if(types!=null){//redis 中有分类数据
            List<Type> ts=(List<Type>)types;
            return ts;
        }

        List<Type> fs = typeDao.getAll();//一级分类的集合

        for (Type f : fs) {
            List<Type> children = typeDao.getChildren(f.getId());//二级分类的集合
            for (Type child : children) {
                List<Type> th = typeDao.getChildren(child.getId());//三级分类集合
                child.setChildren(th);
            }
            f.setChildren(children);

        }
        
        redisTemplate.opsForValue().set("types",fs,1L, TimeUnit.HOURS);


        return fs;
    }

    @Override
    public List<Type> getChildren(Integer id) {
        return null;
    }
}
